package com.fzdy.dataplatform.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fzdy.dataplatform.controller.api.sysLog.SysLogApi;
import com.fzdy.dataplatform.core.dao.Dao;
import com.fzdy.dataplatform.core.service.impl.BaseServiceImpl;
import com.fzdy.dataplatform.core.vo.Page;
import com.fzdy.dataplatform.dao.SyslogDao;
import com.fzdy.dataplatform.entity.Syslog;
import com.fzdy.dataplatform.service.SyslogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SyslogServiceImpl extends BaseServiceImpl<Syslog> implements SyslogService {

    @Autowired
    private SyslogDao syslogDao;

    @Override
    protected Dao getDao() {
        return syslogDao;
    }

    @Override
    public void WriteLog(SysLogApi api) {
        Syslog syslog = new Syslog();
        syslog.setCreatedate(new Date());
        syslog.setUserid(api.getOpreatorid());
        String tittle = api.getOperation() + api.getModule() + api.getStatus();
        syslog.setTitle(tittle);
        //封装content
        StringBuilder contentSB = new StringBuilder();
        JSONObject jsonObject = api.getContent();
        for (Object key : jsonObject.keySet()) {
            String value = jsonObject.getString((String) key);
            contentSB.append(key).append(":").append(value).append("; ");
        }
        syslog.setContent(contentSB.toString());
        syslogDao.save(syslog);
    }

    public void getPageByExampleLike(Syslog query, Page<Syslog> page) {
        String date = query.getContent();
        query.setContent(null);
        List<Syslog> list = syslogDao.findByExampleLike(date, query, page.getPageCurrent(), page.getPageSize());
        long count = syslogDao.countByExampleLike(date,query);
        page.setList(list);
        page.setTotalRow(Long.valueOf(count));
    }

}
